import {Injectable} from '@angular/core';

import {Account} from '../model/account';

import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/delay';

@Injectable()
export class AuthService {
  isLoggedIn: boolean = false;
  // 临时存储URL，登录后直接重定向到请求页面
  redirectUrl: string;
  account: Account;


  login(): Observable<boolean> {
    console.log("执行认证");
    return Observable.of(true).delay(1000).do(val => this.isLoggedIn = true);
  }

  logout(): void {
    this.isLoggedIn = false;
  }

  getAccount(): Account {
    return this.account;
  }

  isLogdedin(): boolean {
    return this.account && this.account.id != null;
  }

  hasRole(role: string): boolean {
    return this.account && this.account.roles.indexOf(role) > 0;
  }
}



